package com.jiezheng.huinong.biz;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class IndexStatisticFileWriter {
	private String fileName = "";
	private String filePath = "";
	private Map<Integer, ArrayList<String>> data = new HashMap<Integer, ArrayList<String>>();

	IndexStatisticFileWriter(String filePath, String fileName) {

		this.setFileName(fileName);
		this.setFilePath(filePath);
	}
	
	/**
	 * 处理文件的写入
	 */
	public void writeToExcel() {
		HSSFWorkbook hssfworkbook = writeProcess();
		OutputStream fileoutputstream = null;
		try {
			fileoutputstream = new FileOutputStream(this.filePath);
			hssfworkbook.write(fileoutputstream);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if( fileoutputstream != null ){
				try {
					fileoutputstream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 设置数据
	 * @param _data
	 */
	public void setData(Map<Integer, ArrayList<String>> _data) {
		if (_data != null) {
			data = _data;
		} else {
			data = new HashMap<Integer, ArrayList<String>>();
		}
	}

	/**
	 * 写入实际的数据
	 */
	private HSSFWorkbook writeProcess() {

		// 创建excel文件对象
		HSSFWorkbook wb = new HSSFWorkbook();
		
		// 创建一个张表
		Sheet sheet = wb.createSheet();
		wb.setSheetName(0, "Sheet");
		
		// 写入文件头
		writeHeader(wb, sheet);
		
		//写入数据
		writeData(wb, sheet);
		
		sheet.autoSizeColumn((short) 0);
		sheet.autoSizeColumn((short) 1);
		sheet.autoSizeColumn((short) 2);
		sheet.autoSizeColumn((short) 3);
		sheet.autoSizeColumn((short) 4);
		sheet.autoSizeColumn((short) 5);
		sheet.autoSizeColumn((short) 6);
		
		return wb;
	}

	/**
	 * 写入文件头
	 * @param wb
	 * @param sheet
	 */
	private void writeHeader(HSSFWorkbook wb, Sheet sheet){
		
		// 创建第一行
		Row row1 = sheet.createRow(0);

		// 文件字体
		Font font0 = createFonts(wb, Font.BOLDWEIGHT_BOLD, "宋体", false,
				(short) 200);
		
		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
		cellStyle.setFont(font0);

		// 给第一行添加文本
		Cell cell = row1.createCell(0);
		cell.setCellValue(" ");
		cell.setCellStyle(cellStyle);
							

		cell = row1.createCell(1);
		cell.setCellValue("文号");
		cell.setCellStyle(cellStyle);
		
		cell = row1.createCell(2);
		cell.setCellValue("指标总金额");
		cell.setCellStyle(cellStyle);
		
		cell = row1.createCell(3);
		cell.setCellValue("已支付金额");
		cell.setCellStyle(cellStyle);
		
		cell = row1.createCell(4);
		cell.setCellValue("剩余金额");
		cell.setCellStyle(cellStyle);
		
		cell = row1.createCell(5);
		cell.setCellValue("用途");
		cell.setCellStyle(cellStyle);
		
		cell = row1.createCell(6);
		cell.setCellValue("预算项目");
		cell.setCellStyle(cellStyle);
	}
	
	private void writeData(HSSFWorkbook wb, Sheet sheet){
		
		Font font1 = createFonts(wb, Font.BOLDWEIGHT_NORMAL, "宋体", false,
				(short) 200);
		
		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
		cellStyle.setFont(font1);
		
		int dataSize = data.size();
		
		// 第二行表示
		int l = 1;
		for (int i = 0; i < dataSize; i++) {
			// 创建一行
			Row rowData = sheet.createRow(l++);
			List<String> line = data.get(i);
			createCell(wb, rowData, 0, line.get(0), cellStyle);
			createCell(wb, rowData, 1, line.get(1), cellStyle);
			createCell(wb, rowData, 2, line.get(2), cellStyle);
			createCell(wb, rowData, 3, line.get(3), cellStyle);
			createCell(wb, rowData, 4, line.get(4), cellStyle);
			createCell(wb, rowData, 5, line.get(5), cellStyle);
			createCell(wb, rowData, 6, line.get(6), cellStyle);
		}		
	}
	
	/**
	 * 设置字体
	 * 
	 * @param wb
	 * @return
	 */
	public static Font createFonts(Workbook wb, short bold, String fontName,
			boolean isItalic, short hight) {
		Font font = wb.createFont();
		font.setFontName(fontName);
		font.setBoldweight(bold);
		font.setItalic(isItalic);
		font.setFontHeight(hight);
		return font;
	}

	/**
	 * 创建单元格并设置样式,值
	 * 
	 * @param wb
	 * @param row
	 * @param column
	 * @param
	 * @param
	 * @param value
	 */
	public static void createCell(Workbook wb, Row row, int column,
			String value, CellStyle cellStyle) {
		Cell cell = row.createCell(column);
		if(column == 3)
		{
			cell.setCellType(Cell.CELL_TYPE_NUMERIC);
			cell.setCellValue(Double.parseDouble(value));
			DataFormat format = wb.createDataFormat();
			cellStyle.setDataFormat(format.getFormat("0.00")); 
			cell.setCellStyle(cellStyle);
		}else
		{
			cell.setCellValue(value);
			cell.setCellStyle(cellStyle);
		}
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
}
